/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.wxApi.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.wxApi.entity.CollectEntity;
import com.pig4cloud.pigx.wxApi.service.CollectService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收藏
 *
 * @author pig
 * @date 2023-11-15 14:19:29
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/collect" )
@Tag(description = "collect" , name = "收藏管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CollectController {

    private final  CollectService collectService;

    /**
     * 通过id查询收藏
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @Inner(value = false)
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(collectService.getById(id));
    }

    /**
     * 新增收藏
     * @param collect 收藏
     * @return R
     */
    @Operation(summary = "新增收藏" , description = "新增收藏" )
    @SysLog("新增收藏" )
    @PostMapping
    @Inner(value = false)
    public R save(@RequestBody CollectEntity collect) {
        LambdaQueryWrapper<CollectEntity> wrapper = Wrappers.lambdaQuery(collect);
        List<CollectEntity> list = collectService.list(wrapper);
        if (list.isEmpty()){
            return R.ok(collectService.save(collect));
        }else {
            return R.ok();
        }

    }

    /**
     * 修改收藏
     * @param collect 收藏
     * @return R
     */
    @Operation(summary = "修改收藏" , description = "修改收藏" )
    @SysLog("修改收藏" )
    @PutMapping
    public R updateById(@RequestBody CollectEntity collect) {
        return R.ok(collectService.updateById(collect));
    }

    /**
     * 通过id删除收藏
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除收藏" , description = "通过id删除收藏" )
    @SysLog("通过id删除收藏" )
    @PostMapping("removeById")
    @Inner(value = false)
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(collectService.removeBatchByIds(CollUtil.toList(ids)));
    }



    @GetMapping("getUserResource/{openId}/{dataTypeId}")
    @Inner(value = false)
    public R getUserResource(@PathVariable("openId" ) String openId,@PathVariable("dataTypeId" ) Long dataTypeId){
        return collectService.getUserResource(openId,dataTypeId);
    }

    @GetMapping("getOpenId/{code}")
    @Inner(value = false)
    public R getOpenId(@PathVariable("code") String code){
        return collectService.getOpenId(code);
    }

    @GetMapping("getCollect/{openId}/{resourceId}")
    @Inner(value = false)
    public String getCollect(@PathVariable("openId" ) String openId,@PathVariable("resourceId" ) Long resourceId){
        return collectService.getCollect(openId,resourceId);
    }

    @GetMapping("/remove/{id}" )
    @Inner(value = false)
    public R remove(@PathVariable("id" ) Long id) {
        LambdaQueryWrapper<CollectEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CollectEntity::getResourceId,id);
        return R.ok(collectService.remove(wrapper));
    }
}
